package com.example.questionexam.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.questionexam.common.ErrorCode;
import com.example.questionexam.constant.CommonConstant;
import com.example.questionexam.exception.BusinessException;
import com.example.questionexam.exception.ThrowUtils;
import com.example.questionexam.mapper.PaperMapper;
import com.example.questionexam.model.dto.paper.PaperQueryRequest;
import com.example.questionexam.model.entity.Paper;
import com.example.questionexam.model.entity.QuestionScore;
import com.example.questionexam.service.PaperService;
import com.example.questionexam.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;

@Service
@Slf4j
public class PaperServiceImpl extends ServiceImpl<PaperMapper, Paper>
        implements PaperService {




    // {} / {{1,2},{2,3}} add {{2,3},{4,3}} / {}
    //-> {{2,3},{4,3}} / {} / {{1,2},{2,3},{4,3}} /{{1,2},{2,3}}

    @Override
    public QueryWrapper<Paper> getQueryWrapper(PaperQueryRequest paperQueryRequest) {
        if (paperQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        Long id = paperQueryRequest.getId();
        String name = paperQueryRequest.getName();
        String sortField = paperQueryRequest.getSortField();
        String sortOrder = paperQueryRequest.getSortOrder();

        QueryWrapper<Paper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(id != null && id != 0, "id", id);
//        queryWrapper.eq(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);

        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);

        return queryWrapper;
    }

    @Override
    public void validPaper(Paper paper, boolean isAdd) {
        ThrowUtils.throwIf(paper == null, ErrorCode.PARAMS_ERROR);

        Long id = paper.getId();
        String name = paper.getName();
        String questions = paper.getQuestions();

        // 创建时，参数不能为空
        if (isAdd) {
            ThrowUtils.throwIf(StringUtils.isAnyBlank(name,questions), ErrorCode.PARAMS_ERROR);
        } else {
            ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        }

        // 有参数则校验
        if (StringUtils.isNotBlank(name) && name.length() > 60) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "试卷名字字数过长");
        }

        if (StringUtils.isNotBlank(questions) && questions.length() >8192) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "题目数目过长");
        }

    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removePaper(Long[] ids) {
        if (ids == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        for (Long id : ids) {
            if (id == 0) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR);
            }
            // 删除
            boolean removeQuestionResult = this.removeById(id);
            if (!removeQuestionResult) {
                throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
            }
        }
        return true;
    }

}
